package com.jeehentor.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeehentor.common.api.SystemCommonAPI;
import com.jeehentor.common.api.vo.LoginUser;
import com.jeehentor.common.api.vo.SystemLog;
import com.jeehentor.dto.MenuDTO;
import com.jeehentor.entity.SysLog;
import com.jeehentor.entity.SysRole;
import com.jeehentor.entity.SysRoleMenu;
import com.jeehentor.entity.SysUser;
import com.jeehentor.system.mapper.SysRoleMapper;
import com.jeehentor.system.mapper.SysRoleMenuMapper;
import com.jeehentor.system.service.ISysLogService;
import com.jeehentor.system.service.ISysMenuService;
import com.jeehentor.system.service.ISysUserService;
import com.jeehentor.utils.SecurityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @Description : 公共接口
 * @Author : wzd
 * @Date : 2024/4/8 14:30
 * @Version : 1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class CommonAPIService implements SystemCommonAPI {

    private final ISysUserService sysUserService;
    private final ISysLogService sysLogService;
    private final ISysMenuService sysMenuService;
    private final SysRoleMapper sysRoleMapper;
    private final SysRoleMenuMapper sysRoleMenuMapper;


    @Override
    public LoginUser findUserByName(String username) {
        return sysUserService.findUserByName(username);
    }

    @Override
    public List<String> getPermissionList(String userId){
        return sysUserService.getPermissionList(userId);
    }

    @Override
    public List<String> getRoleList(String userId) {
        return sysUserService.getRoleList(userId);
    }

    @Override
    public LoginUser getUserById(String id) {
        SysUser byId = sysUserService.getById(id);
        LoginUser loginUser = new LoginUser();
        if(byId != null){
            BeanUtils.copyProperties(byId,loginUser);
        }
        return loginUser;
    }


    @Override
    public void saveLog(SystemLog systemLog) {
        SysLog sysLog = new SysLog();
        BeanUtils.copyProperties(systemLog, sysLog);
        sysLogService.save(sysLog);
    }

    @Override
    public JSONObject addMenu(JSONObject jsonObject){
        MenuDTO menuDTO = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), MenuDTO.class);
        sysMenuService.addMenu(menuDTO);
        return JSONObject.parseObject(JSONObject.toJSONString(menuDTO));
    }

    @Override
    public JSONObject existMenu(String parentId, String name) {
        MenuDTO menuDTO = sysMenuService.existMenu(parentId, name);
        return JSONObject.parseObject(JSONObject.toJSONString(menuDTO));
    }

    @Override
    public void updateMenu(JSONObject jsonObject){
        MenuDTO menuDTO = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), MenuDTO.class);
        sysMenuService.updateMenu(menuDTO);
    }

    @Override
    public void addRoleMenu(List<String> roles, String menuId){
        //删除角色菜单
        if(CollectionUtil.isNotEmpty(roles)){
            LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getMenuId, menuId);
            sysRoleMenuMapper.delete(sysRoleMenuLambdaQueryWrapper);
            roles.forEach(x->{
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
                sysRoleLambdaQueryWrapper.eq(SysRole::getRoleCode,x);
                SysRole role = sysRoleMapper.selectOne(sysRoleLambdaQueryWrapper);
                sysRoleMenu.setRoleId(role.getId());
                sysRoleMenu.setMenuId(menuId);
                // 设置创建者
                sysRoleMenu.setCreateBy(SecurityUtil.getCurrentUser().getId());
                // 设置创建时间
                sysRoleMenu.setCreateTime(LocalDateTime.now());
                sysRoleMenuMapper.insert(sysRoleMenu);
            });
        }
    }
}
